 
package com.zdkj.subway.rca.service.impl;

import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.core.toolkit.Wrappers;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.zdkj.common.core.exceltools.MultipartFileToFile;
import com.zdkj.common.security.service.PigxUser;
import com.zdkj.common.security.util.SecurityUtils;
import com.zdkj.subway.rca.entity.SwCkBomSmallPayment;
import com.zdkj.subway.rca.entity.SwCkBomSmallPaymentAlipayTmp;
import com.zdkj.subway.rca.entity.SwCkBomSmallPaymentWechatTmp;
import com.zdkj.subway.rca.mapper.SwCkBomSmallPaymentMapper;
import com.zdkj.subway.rca.service.SwCkBomSmallPaymentAlipayTmpService;
import com.zdkj.subway.rca.service.SwCkBomSmallPaymentService;
import com.zdkj.subway.rca.service.SwCkBomSmallPaymentWechatTmpService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletResponse;
import java.io.BufferedInputStream;
import java.io.File;
import java.io.IOException;
import java.io.OutputStream;
import java.net.URLEncoder;
import java.time.LocalDate;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;


/**
 * BOM小额支付平台数据汇总表
 *
 * @author stw
 * @date 2020-10-27 20:28:52
 */
@Service
public class SwCkBomSmallPaymentServiceImpl extends ServiceImpl<SwCkBomSmallPaymentMapper, SwCkBomSmallPayment> implements SwCkBomSmallPaymentService {
 
    @Autowired
    private SwCkBomSmallPaymentWechatTmpService swCkBomSmallPaymentWechatTmpService;

    @Autowired
    private SwCkBomSmallPaymentAlipayTmpService swCkBomSmallPaymentAlipayTmpService;
    
    @Override
    public void exportTamplates(String verfiyConfirmFlag, HttpServletResponse response) {
        String verifyConfirmName = null;
        String tableName = null;
       if ("1".equals(verfiyConfirmFlag)) {
           verifyConfirmName = "BOM小额支付平台数据审核-微信";
           tableName = "sw_ck_bom_small_payment_wechat";
       } else if("2".equals(verfiyConfirmFlag)) {
           verifyConfirmName = "BOM小额支付平台数据审核-支付宝";
           tableName = "sw_ck_bom_small_payment_alipay";
       }

        BufferedInputStream br = null;
        OutputStream out = null;
        try {
            response.reset(); // 非常重要
            response.setContentType("application/vnd.ms-excel");
            response.setCharacterEncoding("utf-8");
            final String fileName = URLEncoder.encode(verifyConfirmName, "UTF-8");
            response.setHeader("Content-disposition", "attachment;filename=" + fileName + System.currentTimeMillis() + ".xlsx");

            br =  new BufferedInputStream(this.getClass().getResourceAsStream("/static/excel/template/import/" + tableName + ".xlsx"));

            byte[] buf = new byte[1024];
            int len = 0;

            out = response.getOutputStream();
            while ((len = br.read(buf)) > 0) {
                out.write(buf, 0, len);
            }

        } catch (Exception e) {
            e.printStackTrace();
        } finally {
            if(out != null) {
                try {
                    out.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
            if(br != null){
                try {
                    br.close();
                } catch (IOException ioe) {
                    ioe.printStackTrace();
                }
            }
        }
    }

    @Override
    public Boolean saveBatchWithExcel(MultipartFile file, String verfiyConfirmFlag) throws Exception {

        File excelFile = MultipartFileToFile.multipartFileToFile(file);
        if("1".equals(verfiyConfirmFlag)) {
            swCkBomSmallPaymentWechatTmpService.saveBatchWithExcel(excelFile);
        } else if("2".equals(verfiyConfirmFlag)) {
            swCkBomSmallPaymentAlipayTmpService.saveBatchWithExcel(excelFile);
        }

        return true;
    }

    @Override
    public List<Map<String,String>> getSwCkBomSmallPaymentConfirmTableList() {

        List<Map<String,String>> listMap = new ArrayList<>();
        Map<String,String> mapWechat = new HashMap<>();
        mapWechat.put("verfiyConfirmFlag","1");
        mapWechat.put("verifyConfirmName","BOM小额支付平台数据审核-微信");
        SwCkBomSmallPaymentWechatTmp swCkBomSmallPaymentWechatTmp = new SwCkBomSmallPaymentWechatTmp();
        PigxUser user = SecurityUtils.getUser();
        swCkBomSmallPaymentWechatTmp.setCreatorId(user.getId());
        if(swCkBomSmallPaymentWechatTmpService.count(Wrappers.query(swCkBomSmallPaymentWechatTmp)) > 0 ){
            mapWechat.put("importStatus","未确认");
        } else {
            mapWechat.put("importStatus","已确认或已取消");
        }
        listMap.add(mapWechat);

        Map<String,String> mapAlipay = new HashMap<>();
        mapAlipay.put("verfiyConfirmFlag","2");
        mapAlipay.put("verifyConfirmName","BOM小额支付平台数据审核-支付宝");
        SwCkBomSmallPaymentAlipayTmp swCkBomSmallPaymentAlipayTmp = new SwCkBomSmallPaymentAlipayTmp();

        swCkBomSmallPaymentAlipayTmp.setCreatorId(user.getId());
        if(swCkBomSmallPaymentAlipayTmpService.count(Wrappers.query(swCkBomSmallPaymentAlipayTmp)) > 0 ){
            mapAlipay.put("importStatus","未确认");
        } else {
            mapAlipay.put("importStatus","已确认或已取消");
        }
        listMap.add(mapAlipay);
        return listMap;
    }
    @Override
    public List<Map<String,String>> selectLineMap() {
        return this.baseMapper.selectLineMap();
    }
    @Override
    public List<Map<String,String>> selectStationMap() {
       return this.baseMapper.selectStationMap();
    }
    @Override
    public List<Map<String,String>> selectTicketTtypeMap() {
        return this.baseMapper.selectTicketTtypeMap();
    }

    @Override
    public List<Map<String, Object>> getSwCkBomSmallCount(LocalDate startDateLocalDate, LocalDate endDateLocalDate,String lineCode) {
        return this.baseMapper.getSwCkBomSmallCount(startDateLocalDate,endDateLocalDate,lineCode);
    }

    @Override
    public List<Map<String, Object>> getCashSale(LocalDate startDateLocalDate, LocalDate endDateLocalDate,String lineCode) {
        return this.baseMapper.getCashSale(startDateLocalDate, endDateLocalDate,lineCode);
    }

    @Override
    public List<Map<String, Object>> getAllSale(LocalDate startDateLocalDate, LocalDate endDateLocalDate,String lineCode) {
        return this.baseMapper.getAllSale(startDateLocalDate, endDateLocalDate,lineCode);
    }

    @Override
    public IPage<SwCkBomSmallPayment> selectPage(Page page, String startDate, String endDate) {
        return this.baseMapper.selectPage(page, startDate, endDate);
    }

    @Override
    public void updateBatch(List<Integer> integerList) {
        this.baseMapper.updateBatch(integerList);
    }

}
